LeosLyrics = function()
{
  this.search_result;
  this.lyric_text = [];
  this.ajax =  new Ajax();//(typeof ajax == 'object')? ajax : new Ajax();
  this.target;
  this.art = '';
  this.tle = '';
  this.MAX = 10;
  this.total_pages;
  this.current_page=0;
  this.displayList = function(){
    var html_content = '<p>Couldn\'t find an exact match...<br />have a look at our results</p>';
    var start_index = this.current_page ? this.MAX*this.current_page : 0;
    
    html_content += this.displayNav(start_index+this.MAX);

    for(var i=start_index; i<start_index+this.MAX; i++){
      if(this.results.length <= i) break;

      var title = this.results[i].getElementsByTagName('title')[0].firstChild.nodeValue;
      var artist = this.results[i].getElementsByTagName('artist')[0].getElementsByTagName('name')[0].firstChild.nodeValue;
      try{      
        var feat = this.results[i].getElementsByTagName('feat')[0].firstChild.nodeValue
      }catch(e) {var feat = ''; }
      var hid = this.results[i].getAttribute('hid');   

      html_content += '<a href="javascript:lyrics.getLyric(\''+encodeURIComponent(hid)+'\')"><b>'+title+'</b></a>, by '+artist;
      if (feat.length) html_content += ', '+feat;
      html_content += '<br />';      
    }
    return html_content;
  }

  this.displayNav = function(end){
    if(this.results.length <= this.MAX) return '';

    this.current_page = (parseInt(end/this.MAX) + (end%this.MAX?1:0)) - 1;
    var html = '<p class="nav">';

    for(var p=0; p<this.total_pages; p++){
        if(this.current_page != p)
          html += '<a href="javascript:lyrics.viewPage('+p+')">'+(p+1)+'</a>';
        else
          html += (p+1);
        if(p != this.total_pages -1) html +=' | ';
    }

    return html+'</p>';
 }
}

LeosLyrics.inherits(WebService);

LeosLyrics.prototype.viewPage = function(number)
{
  this.current_page = number;
  this.target.innerHTML = this.displayList();
}

LeosLyrics.prototype.search = function(artist, title, back)
{
  if(back) { // we go back to the previous search results
    this.target.innerHTML = this.show(0,1);
    return;  
  }

  this.art = artist;
  this.tle = title;
  var l = this;
  var options = {target: l.target, timeout: 0, 
          onLoading:function(o,target){ onloading(target);},
          onError: function(){l.error();},
          onComplete:function(o,target){ if(!l.checkResponse(o)) return; l.search_result = o.responseXML; target.innerHTML = l.show();},        
          data: 'artist='+encodeURIComponent(this.art)+'&title='+encodeURIComponent(this.tle)+'&op=lyrics'};

  this.ajax.makeRequest('proxy.php', options);

  this.lyric_text = [];
} 

LeosLyrics.prototype.getLyric = function(lyric_id)
{
  
  if(this.lyric_text[lyric_id] != null){
    this.target.innerHTML = this.show(lyric_id); 
    return;
  }

  var l = this;  
  var options = {target: l.target, timeout: 0, 
          onLoading:function(o,target){ onloading(target);},
          onError: function(o,target,str){l.uber('error',null,null,str); },
          onComplete:function(o,target){ if(!l.checkResponse(o)) return; l.lyric_text[lyric_id] = o.responseXML; target.innerHTML = l.show(lyric_id);},        
          data: 'hid='+encodeURIComponent(lyric_id)+'&op=lyrics'};

   this.ajax.makeRequest('proxy.php', options);

}

LeosLyrics.prototype.show = function(lyric_id,cover_)
{ 
  
  if(lyric_id){
    try{var lyric = this.lyric_text[lyric_id].getElementsByTagName('lyric');}
      catch(e){ return 'Lyric not found...try <a target="_blank" href="http://www.leoslyrics.com/listlyrics.php?hid='+encodeURIComponent(lyric_id)+'">here</a>'; }
  }
  else
    var lyric = this.search_result.getElementsByTagName('lyric');

  if(lyric.length == 1){//we have an exact match
    
    var text = lyric[0].getElementsByTagName('text')[0];
    var title = lyric[0].getElementsByTagName('title')[0].firstChild.nodeValue;
    var hid = lyric[0].getAttribute('hid');
    var artist = lyric[0].getElementsByTagName('artist')[0].getElementsByTagName('name')[0].firstChild.nodeValue;
    try{
      var album = lyric[0].getElementsByTagName('albums')[0].getElementsByTagName('album')[0].getElementsByTagName('name')[0].firstChild.nodeValue;
      
      //fetchs the album cover (it does it now that we have the album name!)
      if(!lyric_id && !cover_ && window.cover) cover.search(artist, album, 1);
    }catch(e) {var album = ''; if(!lyric_id && !cover_ && window.cover) cover.search(this.art, this.tle);}
    /*    
    try{
      var img = lyric[0].getElementsByTagName('albums')[0].getElementsByTagName('album')[0].getElementsByTagName('imageUrl')[0].firstChild.nodeValue;
    }catch(e) {var img = ''; }
    */
    try{
      var feat = lyric[0].getElementsByTagName('feat')[0].firstChild.nodeValue;
    }catch(e) {var feat = ''; }
    var html_content = (lyric_id ? '<p><a href="javascript:lyrics.search(0,0,1)">Get back to the results</a></p>':'')
      +'<p><a target="_blank" href="http://www.leoslyrics.com/listlyrics.php?hid='+encodeURIComponent(hid)+'"><b>'+title+'</b></a>';

    if(album.length) html_content += ' from <i>'+album+'</i>';
        html_content +=  '</p><p>by '+artist;
    if(feat.length)  html_content += ','+feat;
        html_content += '</p>';
    //if(img.length)   html_content += '<img src="'+img+'" /><br />'
        html_content += '<br /><p id="lyric_text">'; 
        for(var child = 0; child < text.childNodes.length; child++)
            html_content += text.childNodes[child].nodeValue;
        html_content += '</p>';

    return html_content;
  }
  
  this.results = this.search_result.getElementsByTagName('result');
  this.total_pages = parseInt(this.results.length/this.MAX) + (this.results.length%this.MAX?1:0);

  if(this.results.length){ //we have a list
    if(!lyric_id && !cover_ && window.cover) cover.search(this.art, this.tle);
    return this.displayList();

  }

  return "<strong>No lyrics found...</strong>"; //very unlikely...
}
LeosLyrics.prototype.error = function(){
 this.uber('error');
 if(typeof window.cover == 'object') cover.search(this.art, this.tle);
}
